﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;

namespace Skyhook.AI.PathFinder
{
    public class BlockingMap
    {
        bool[][] Map = null;
        int pWidth = 0;
        int pHeight = 0;
        internal BlockedPoint FirstBlockedPoint = null;

        public BlockingMap(int width, int height)
        {
            pWidth = width;
            pHeight = height;

            Map = new bool[pWidth][];

            for (int j = 0; j < pWidth; j++)
            {
                Map[j] = new bool[pHeight];
            }

            for (int i = 0; i < pHeight; i++)
            {
                for (int j = 0; j < pWidth; j++)
                {
                    Map[j][i] = false;
                }
            }
        }
        public void SetBlocked(int x, int y)
        {
            if (!IsBlocked(x, y))
            {
                Map[x][y] = true;
                BlockedPoint point = new BlockedPoint();
                point.X = x;
                point.Y = y;
                point.Next = FirstBlockedPoint;
                point.Previous = null;
                if (FirstBlockedPoint != null)
                {
                    FirstBlockedPoint.Previous = point;
                }
                FirstBlockedPoint = point;
            }
        }
        public void SetUnblocked(int x, int y)
        {
            if (IsBlocked(x, y))
            {
                Map[x][y] = false;
                BlockedPoint point = FirstBlockedPoint;
                while (point != null)
                {
                    if (point.X == x && point.Y == y)
                    {
                        if (point.Next != null)
                        {
                            point.Next.Previous = point.Previous;
                        }
                        if (point.Previous != null)
                        {
                            point.Previous.Next = point.Next;
                        }
                        break;
                    }
                    point = point.Next;
                }
            }
        }

        public bool IsBlocked(int x, int y)
        {
            if (x >= pWidth || y >= pHeight || x < 0 || y < 0)
            {
                return true;
            }

            return Map[x][y];
        }

        public int Width
        {
            get
            {
                return pWidth;
            }
        }

        public int Height
        {
            get
            {
                return pHeight;
            }
        }
    }
}
